package com.feiyuan.book.app;

import com.feiyuan.book.service.BookChapterService;
import com.feiyuan.book.service.BookContentService;
import com.feiyuan.book.service.BookListService;
import com.feiyuan.book.service.BookUserShelfService;
import com.feiyuan.book.vo.*;
import com.feiyuan.common.annotation.Login;
import com.feiyuan.common.annotation.LoginUser;
import com.feiyuan.common.entity.UserEntity;
import com.feiyuan.common.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * 搜索笔趣阁图书控制类
 */
@RestController
@RequestMapping("/app/bqg")
public class SearchBQGAppController {

    @Autowired
    private BookListService bookListService;

    @Autowired
    private BookContentService bookContentService;

    @Autowired
    private BookUserShelfService bookUserShelfService;

    /**
     * 搜索书名或者作者名
     * @param params 书名或者作者名
     * @return R
     */
    @GetMapping("/searchBookOrAuthor")
    public R searchBookOrAuthor(@RequestParam(name = "params", required = true) String params) {
        List<SearchBookVO> bookVOS = bookListService.searchBQGBook(params);
        return R.ok().put("books", bookVOS);
    }

    /**
     * 根据笔趣阁上的书籍地址得到书籍的详细信息以及章节列表
     * @param bookUrl 书籍地址
     * @return R
     */
    @GetMapping("/bookInfoAndChapters")
    public R bookInfoAndChapters(@RequestParam(name = "bookUrl") String bookUrl, @RequestParam(name = "isLogin") Boolean isLogin, HttpServletRequest request) {
        BookInfoVO bookInfoVO = bookListService.bqgBookInfo(bookUrl, isLogin, request);
        return R.ok().put("bookInfo", bookInfoVO);
    }

    /**
     * 加入书架
     * @param bookInfo 书籍内容
     * @return R
     */
    @PostMapping("/joinShelf")
    @Login
    public R joinShelf(@RequestBody BookInfoVO bookInfo, @LoginUser UserEntity user) {
        Object obj = bookListService.bqgJoinShelf(bookInfo, user);
        return R.ok().put("isSuccess", obj);
    }

    /**
     * 开始阅读
     * @param bookInfo 书籍内容
     * @return R
     */
    @PostMapping("/startRead")
    public R startRead(@RequestBody BookInfoVO bookInfo, @LoginUser UserEntity user) {
        return R.ok().put("isSuccess", bookListService.bqgJoinShelf(bookInfo, user));
    }

    /**
     * 获取内容
     * @param bookChapter 章节信息
     * @return R
     */
    @PostMapping("/getContent")
    public R getContent(@RequestBody BookChapterVO bookChapter) {
        BookContentVO bookContent = bookContentService.getBqgContent(bookChapter);
        return R.ok().put("content", bookContent);
    }

    /**
     * 查询书籍章节和章节内容
     * @param bookContent 书籍章节网络ID和书籍网络ID
     * @return R
     */
    @PostMapping("/queryChapterAndContent")
    public R queryChapterAndContent(@RequestBody BookContentVO bookContent) {
        Map<String, Object> map = bookContentService.queryBqgChapterAndContent(bookContent);
        return R.ok(map);
    }

    /**
     * 从阅读页返回时，将用户读到的章节ID和章节翻页下标更新到书架表中
     * @return R
     */
    @PostMapping("/backPress")
    @Login
    public R backPress(@LoginUser UserEntity user, @RequestBody BookShelfVO bookShelf) {
        Map<String, Object> map = bookUserShelfService.backPress(user.getUserId(), bookShelf);
        return R.ok(map);
    }
}
